<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Home\Controller;
use Think\Controller;
use Org\Util\Juhe;
class AgentController extends HomeController {  
    public function agent() {
             $userid = session('userid');   
             $sql="select a.city,a.street,a.shopName,a.realName,a.phone from gr_buyers a where a.id = $userid ";
             $res=D('buyers')->query($sql);
             $addr=$res[0]['city'].$res[0]['street'].$res[0]['shopname'];
              $this->rejectinfo=$res;
              $this->assign("realname",$res[0]['realname']);
              $this->assign("phone",$res[0]['phone']);
              $this->assign("addr",$addr);
              $this->display();

    }
    public function saveAgent() {
        $userid = session('userid');   
        $map = array('uid' => $userid);
        $smslist="";
        if (IS_POST) {
                    $agent_order="DG".time()."".rand(0, 10);
                    $user_name=$_POST['username'];
                    $user_tel=$_POST['usertel'];
                    $user_addr=$_POST['useraddr'];
                    $describe = $_POST['describe'];
                    $data = array('uid' => $userid,'orderid' => $agent_order, 'create_time'=>time(),'describe' => $describe,'user_name'=>$user_name,'user_tel'=>$user_tel,'user_addr'=>$user_addr);
                    $reszsm=M('agent_order')->add($data);
                    if($reszsm){
                        $goods_length=count($_POST['agent_name']);
                        $data2['orderid']=$agent_order;
                        for($i=0;$i<$goods_length;$i++){
                            $data2['pname']=$_POST['agent_name'][$i];
                            $data2['num']=$_POST['agent_num'][$i];
                            $data2['ordernum']=$_POST['agent_num'][$i];
                            $data2['price']=$_POST['agent_price'][$i];
                            $smslist.=$data2['pname']."*".$data2['num'].",";
                           M('agent_order_goods')->add($data2);
                        }
                                $labelTable=D('config');
                                $where['name']='DGMOBILE';
                                $labellist=$labelTable->where($where)->getfield('value');
                                $str= explode("\n", $labellist);
                                $arr=array();
                                foreach ($str as $value){
                                   $arr2=explode(":", $value);
                                   if($arr2[0]==""||$arr2[0]==" "||$arr2[0]==null){  
                                           }else{
                                           $arr[$arr2[0]]=trim($arr2[1]);
                                   }
                                }
//                                var_dump($arr);
                                foreach ($arr as $value){
                                   $mobile=$value;
                                   $JUHE= new Juhe();
                                   $tpl_id=4529;       
                                   $tpl_value = "#price#=" . '33' . "&#list#=" . $smslist;
                                   $JUHE->sendSMS($mobile, $tpl_id, $tpl_value);
                                }
                         $this->display();
                    }
                    
                //}
                
//               $this->display('Registration/regSuccess');       
            }else{
                
            }
           
        
        
     }  
    public function myAgent(){        
        $map = array('uid'=>session("userid"));
        $order=M('agent_order');
        $orderlist=$order->where($map)->order('create_time desc')->select(); 
        $count=count($orderlist);
        for($i=0;$i<$count;$i++){
            $data2['orderid']=$orderlist[$i]['orderid'];
            $goodlist=M('agent_order_goods')->where($data2)->select();
            $orderlist[$i]['good_details']=$goodlist;
        }
        $this->assign('historyList',$orderlist);
        $this->display();
    }   
    public function ajax(){  
        if(IS_POST){
            $map['name']= $_POST['pname'];
            $map['status']=1;
            $price=M('product')->where($map)->getField('price');
            if($price){
                echo $price;
            }else{
                echo "";
            }
        }
    }   
    public function cancelOrder($id){
        $orderid=$id;
        //echo $orderid;
        //$this->success('新增成功', 'Center/history');       
            if(isset($orderid)){
                $map = array('orderid'=>$orderid);
                $data['status']= -1;
                $result= M('agent_order')->where($map)->save($data);   
                if($result){
                    $this->redirect('Agent/myagent');
                }else{
                    
                }            
            }                
    }
        
}
